package com.hk.jmm.presenter;

import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;

import androidx.lifecycle.Lifecycle;

import com.hjq.toast.ToastUtils;
import com.hk.jmm.R;
import com.hk.jmm.app.LoginActivity;
import com.hk.jmm.model.LoginByPasswordModel;
import com.skyz.base.mvp.BasePresenter;
import com.skyz.base.mvp.IModel;
import com.skyz.base.util.NetWorkUtil;
import com.skyz.wrap.manager.TokenManager;
import com.skyz.wrap.utils.CountDownTimerUtils;

public class LoginByPasswordPresenter extends BasePresenter<LoginByPasswordModel, LoginActivity> {

    CountDownTimerUtils countDownTimerUtils;
    public LoginByPasswordPresenter(LoginActivity view, Lifecycle lifecycle) {
        super(view, lifecycle);
    }

    @Override
    protected LoginByPasswordModel initMvpModel() {
        return new LoginByPasswordModel();
    }

    private void loginByPass() {
       String account = getMvpView().et_account.getText().toString();
       String pass = getMvpView().et_pass.getText().toString();
        if(TextUtils.isEmpty(account)){
            ToastUtils.show("请输入账号");
            return;
        }
        if(TextUtils.isEmpty(pass)){
            ToastUtils.show("请输入密码");
            return;
        }
    }

    private void loginByCode() {
        String phone = getMvpView().et_phone.getText().toString();
        if(phone.length()!=11){
            ToastUtils.show("请输入正确的手机号");
            return;
        }
        String code = getMvpView().et_code.getText().toString();
        if(code.length()!=4){
            ToastUtils.show("请输入正确的验证码");
            return;
        }

    }

    public void login() {
        if(getMvpView().getPassLogin()){
            loginByPass();
        }else {
            loginByCode();
        }

    }

    public void sendCode() {
        if(countDownTimerUtils==null){
            countDownTimerUtils =new CountDownTimerUtils( getMvpView().click_send_code);
        }
        String phone = getMvpView().et_phone.getText().toString();
        if(phone.length()!=11){
            ToastUtils.show("请输入正确的手机号");
            return;
        }
        countDownTimerUtils.start();
    }


    public void onDestroy(){
        if(countDownTimerUtils!=null){
            countDownTimerUtils.cancel();
            countDownTimerUtils=null;
        }
    }



    //登录接口
    private void apiLogin(String username, String pwd) {
        getMvpModel().showPhoneLogin(username, pwd, new IModel.ModelCallBack<String>() {
            @Override
            public void onSuccess(String token) {
                TokenManager.getInstance().setToken(getMvpView().getApplicationContext(), token);
                String lastUsername = TokenManager.getInstance().getUserName(getMvpView().getApplicationContext());
                boolean isSameAccount = TextUtils.isEmpty(lastUsername) || username.equals(lastUsername);

                TokenManager.getInstance().setUserName(getMvpView().getApplicationContext(), username);

            }

            @Override
            public void newWorkEable() {

            }
        });
    }

    public void sysShow() {
        TransformationMethod method = getMvpView().et_pass.getTransformationMethod();
        boolean isHid = method==null || method.equals(HideReturnsTransformationMethod.getInstance());
       if(isHid){
           getMvpView().sys_btn_show.setImageResource(R.mipmap.sys_btn_show);
           getMvpView().et_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
       }else {
           getMvpView().sys_btn_show.setImageResource(R.mipmap.sys_btn_hide);
           getMvpView().et_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
       }
    }
}
